package com.wanmait.hotelManagement.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.hotelManagement.vo.Admin;

/**
 * Servlet Filter implementation class IsLoginFilter
 */
@WebFilter("/admin/frame.jsp")
public class IsLoginFilter implements Filter {

    /**
     * Default constructor. 
     */
    public IsLoginFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest)request;
//		String uri = httpRequest.getRequestURI();
//		if(uri.contains(".css")||uri.contains(".js")||uri.contains(".png")||uri.contains(".jpg")||uri.contains(".eot")||uri.contains(".svg")||uri.contains(".ttf")||uri.contains(".woff")||uri.contains(".gif")) {
//			chain.doFilter(httpRequest, response);
//			return;
//		}
		
		HttpSession session = httpRequest.getSession();
		Admin admin = (Admin)session.getAttribute("admin");
		if(admin==null) {

			HttpServletResponse httpResponse = (HttpServletResponse)response;
			httpResponse.sendRedirect(httpRequest.getContextPath()+"/hotel/Background-Login/index.jsp");
		}else {
			chain.doFilter(httpRequest, response);
		}
		
		
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
